var vm = new Vue({
    el:'#wrapper',
    data(){
        return{
            list:[{
                "id": "",
                "courseId": "",
                "courseName": "操作系统",
                "classTime": "2022-09-07T14:11:37",
                "isDelete": "0",
                "endTime": "2022-10-07T14:11:37",
                "number": 0,
                "maxNumber": 5000,
                "credits": 2,
                "status": "1",
                "createTime": "2022-09-07T14:11:37",
                "createUser": "202200001",
                "updateTime": "2022-09-07T14:11:37",
                "updateUser": "202200001",
                "teacherList":[],
                "mainTeacherName": "伍德",
            }],
            current:1,
            number:"",
            rolee:""
        }
    },
    methods:{
        deleteCourse(item){
            console.log(item)
            var $this = this;
            $.ajax({
                url:"/course/delete",
                type:"get",
                dataType:"json",
                data:{"courseId":item},
                async:false,
                success(data){
                    $this.$message.success("删除成功！");
                    var current = $("#current").text();
                    getPage(current);
                },
                error(){
                    $this.$message.error("删除失败！");
                }
            })
        },
        updateStatus(item,status){
            var $this = this;
            if(item.teacherName === "暂时未安排老师！"){
                $this.$message.error("课程未安排老师，不能启用！");
                return;
            }
            $.ajax({
                url:"/course/updateStatus",
                dataType: "json",
                type:"put",
                data:{"courseId":item.courseId,"status":status},
                async: "false",
                success(data) {
                    $this.$message.success("状态修改成功！");
                    getPage($this.current)
                },
                error() {
                    $this.$message.error("状态修改失败！");
                }

            })
        },
        jump(){
            var current = $("#page").val();
            console.log("jump:"+current);
            if(parseInt(current) < 1 || parseInt(current) >= parseInt(lastPage)+1 || parseInt(current) == NaN
                || parseInt(current) == undefined){
                vm.$message.error("你输入的数字不在范围内！");
                return
            }
            getPage(current);
        },
        toUpdate(courseId){
            location.href = "course-message.html?courseId="+courseId;
        },
        toAdd(){
            location.href = "course-add.html";
        },
        chooseCourse(id){
            console.log(id)
        }
    }
});

$(function (){

    getPage(vm.current);
    var lastPage = $("#lastPage").text();
    $("#homePage").click(function (){
        getPage(1);
    })
    $("#backPage").click(function (){
        getPage(lastPage);
    })
    $("#next").click(function (){

        lastPage = $("#lastPage").text();
        vm.current++;
        if(vm.current > parseInt(lastPage)){
            vm.current = lastPage;
            return;
        }
        getPage(vm.current);
    })
    $("#prev").click(function (){
        vm.current--;
        if(vm.current < 1){
            vm.current = 1;
            return
        }
        getPage(vm.current);
    })
    $("#courseSearch").click(function (){
        getPage(vm.current);
    })

    $("#start").click(function (){
        var start = $(this);

        var flag = $(this).prev().text() === "暂时未安排老师！";
        var status = start.val();
        var courseId = start.parent().parent().children(".cId").text();
        if(flag){
            alert("请先为课程安排老师！");
            return
        }

    })
    })

    function getPage(current){
            var courseId = $("#courseId").val();
            var courseName = $("#courseName").val();
            $.ajax({
                url: "/course/courses",
                type: "get",
                dataType:"json",
                data:{"current":parseInt(current),"size":8,"courseId":courseId,"courseName":courseName},
                async:false,
                success(data){
                    vm.list = data.data.course.records;

                    $("#current").html(" &nbsp;"+data.data.course.current+"&nbsp; ");
                    $("#lastPage").html(data.data.course.pages);

                },
                error(){
                    vm.$message.error("数据获取失败！");
                }
            })

}

